package net.borderwars.terrain;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import static java.lang.System.currentTimeMillis;

/**
 * @author ehubbard
 *         Date: Aug 22, 2005
 *         Time: 7:40:00 PM
 */
public class TerrainViewer {
    private JButton button1;
    private JButton button2;
    private static final int WIDTH = 640;
    private static final int HEIGHT = 480;
    private static JFrame frame;
    private JPanel mainPanel;
    private JPanel mapArea;
    private FractalTerrain bigTerrain;

    public static void main (String[] args) {
        TerrainViewer tw = new TerrainViewer ();
        frame = new JFrame ("Borderwars Client");
        frame.setContentPane (tw.mainPanel);
        centerFrame (frame, WIDTH, HEIGHT);
        frame.setDefaultCloseOperation (JFrame.DISPOSE_ON_CLOSE);
        frame.show ();
    }

    public TerrainViewer () {
        button1.addActionListener (new ActionListener() {
            public void actionPerformed (ActionEvent e) {
                draw ();
            }

        });
        mapArea.addMouseListener (new MouseAdapter() {
            public void mouseClicked (MouseEvent e) {
                System.err.println ("encoder.x encoder.y " + e.getX () + "," + e.getY ());
                drawDetailed (e.getX (), e.getY ());
            }
        });
    }

    private FractalTerrain littleTerrain = null;

    private void drawDetailed (int xx, int yy) {
        long delta = currentTimeMillis ();

        double start = bigTerrain.getAltitude (xx, yy);
        littleTerrain = new FractalTerrain (start);
        System.err.println ("Generation time " + (currentTimeMillis () - delta) + "ms");
        drawOnPanel (littleTerrain, mapArea, 300, 0);

    }

    private void draw () {
        long delta = currentTimeMillis ();
        int count = 10;
        for (int x = 0; x < count; x++) {
            bigTerrain = new FractalTerrain ();
        }
        System.err.println ("Generation time " + (currentTimeMillis () - delta) / count + "ms per map");
        drawOnPanel (bigTerrain, mapArea, 0, 0);

    }

    private void drawOnPanel (FractalTerrain ft, JPanel area, int sx, int sy) {
        BufferedImage bi = new BufferedImage (256, 256, BufferedImage.TYPE_3BYTE_BGR);
        Graphics g = bi.getGraphics ();
        for (int x = 0; x < 256; x++) {
            for (int y = 0; y < 256; y++) {
                FractalTerrain.RGB rgb = ft.getColor (x, y);
                Color c = rgb.getColor ();
                g.setColor (c);
                g.drawRect (x, y, 1, 1);
            }
        }
        area.getGraphics ().drawImage (bi, sx, sy, null);
    }

    private static void centerFrame (JFrame frame, int w, int h) {
        Dimension d = Toolkit.getDefaultToolkit ().getScreenSize ();
        int sx = (d.width - w) >> 1;
        int sy = (d.height - h) >> 1;
        frame.setLocation (sx, sy);
        frame.setSize (w, h);
    }
}
